<%@ page language="java" contentType="text/html; charset=UTF-8" import="java.util.*,
com.brother.model.*,com.brother.service.*,com.brother.realtime.*,com.brother.realtime.config.*
,com.brother.realtime.config.thirdparty.*
,com.brother.realtime.cache.*"   pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%@ include file="../international.jsp" %> 
<jsp:useBean id="alarm" class="com.brother.model.Alarm"></jsp:useBean>
<jsp:useBean id="alarmService" class="com.brother.service.AlarmService"></jsp:useBean>
<jsp:setProperty property="*" name="alarm"/>
<%
com.brother.model.Customer user = (com.brother.model.Customer)session.getAttribute(com.brother.Constants.LOGIN_SESSTION_NAME);
if(user == null || user.getUserid() == 0 || !user.isAdmin())
	return;
	String action = request.getParameter("action");
String trackerId = request.getParameter("trackerId");
String data = request.getParameter("data");
String work = request.getParameter("work");



if(action != null){
	TrackerService tks =new TrackerService();
	AlarmService als = new AlarmService();
	if(action.equalsIgnoreCase("reportInterval")){
		if(data.matches("^\\d+$")){
	tks.updateTrackerReportInterval(trackerId, (Float.parseFloat(data))+"");
		 	ConfigReportInterval cfg = new ConfigReportInterval();
		 	cfg.setInterval(Integer.parseInt(data));
		 	cfg.setWork(work);
		 	//System.out.println("trackerId :" + trackerId +"  " +ProcessorPool.getInstance().getProcessorByTrackerId(trackerId));
			if(ProcessorPool.getInstance().getProcessorByTrackerId(trackerId) != null){
				ProcessorPool.getInstance().getProcessorByTrackerId(trackerId).addConfig(cfg);
			}
	
			ActionData adata = new ActionData();
			adata.setTrackerId(trackerId);
			adata.setAction("reportInterval");
			
			ParametersSetCache.getInstance().addAction(adata);
		}	
	}else if(action.equalsIgnoreCase("geofenceAdd")){
		if(alarm.getData() == null || alarm.getData().indexOf(";") == -1){
			response.sendRedirect("sysgeofence_edit.jsp?alarmId="+alarm.getAlarmId()+"&action=geofenceAdd&trackerId="+alarm.getTrackerId());
			return;
		}
		als.addGeoFenceAlarm(alarm);
		response.sendRedirect("sysgeofence.jsp?trackerId="+trackerId);
		com.brother.database.DataBase.closeThreadConnection();
		return;
	}else if(action.equalsIgnoreCase("geofenceEdit")){
		if(alarm.getData() == null || alarm.getData().indexOf(";") == -1){
			response.sendRedirect("sysgeofence_edit.jsp?alarmId="+alarm.getAlarmId()+"&action=geofenceEdit&trackerId="+alarm.getTrackerId());
			return;
		}
		als.updateGeoFenceAlarm(alarm); 
		response.sendRedirect("sysgeofence.jsp?trackerId="+trackerId);
		com.brother.database.DataBase.closeThreadConnection();
		return;
	}else if(action.equalsIgnoreCase("speed")){
		System.out.println(action+"  000fdf:data:" + data);
		
		if(data.matches("^\\d+\\.?\\d+$")){
			als.addSpeedAlarm(trackerId,Float.parseFloat(data), work); 
			ActionData adata = new ActionData();
			adata.setTrackerId(trackerId);
			adata.setAction("speed");
			ParametersSetCache.getInstance().addAction(adata);
		}
	}else if(action.equalsIgnoreCase("engine")){
		Alarm al = new Alarm();
		
		al.setAlarmType(AlarmType.Engine.name());
		al.setCreateTime(new Date());
		al.setState(data);
		al.setTrackerId(trackerId);
		alarmService.addOrUpdateAlarm(al);
		
		EngineConfig cfg = new EngineConfig();
	 	cfg.setAction(work);
		if(ProcessorPool.getInstance().getProcessorByTrackerId(trackerId) != null){
			ProcessorPool.getInstance().getProcessorByTrackerId(trackerId).addConfig(cfg);
		}
		ActionData adata = new ActionData();
		adata.setTrackerId(trackerId);
		adata.setAction("engine");
		ParametersSetCache.getInstance().addAction(adata);
	}else if(action.equalsIgnoreCase("numbera")){
		
		Alarm al = new Alarm();
		
		al.setAlarmType(AlarmType.Number_A.name());
		al.setCreateTime(new Date());
		al.setData(data);
		al.setState(work.equals(NumberAConfig.ENABLE) ? "Enabled":"Disabled");
		al.setTrackerId(trackerId);
		alarmService.addOrUpdateAlarm(al);
		
		NumberAConfig cfg = new NumberAConfig();
	 	cfg.setWork(work);
	 	cfg.setTel(data);
		if(ProcessorPool.getInstance().getProcessorByTrackerId(trackerId) != null){
			ProcessorPool.getInstance().getProcessorByTrackerId(trackerId).addConfig(cfg);
		}
		ActionData adata = new ActionData();
		adata.setTrackerId(trackerId);
		adata.setAction("numbera");
		ParametersSetCache.getInstance().addAction(adata);		
	}else if(action.equalsIgnoreCase("numberb")){
		
		Alarm al = new Alarm();
		
		al.setAlarmType(AlarmType.Number_B.name());
		al.setCreateTime(new Date());
		al.setData(data);
		al.setState(work.equals(NumberBConfig.ENABLE) ? "Enabled":"Disabled");
		al.setTrackerId(trackerId);
		alarmService.addOrUpdateAlarm(al);
		
		NumberBConfig cfg = new NumberBConfig();
	 	cfg.setWork(work); 
	 	cfg.setTel(data);
		if(ProcessorPool.getInstance().getProcessorByTrackerId(trackerId) != null){
			ProcessorPool.getInstance().getProcessorByTrackerId(trackerId).addConfig(cfg);
		}
		ActionData adata = new ActionData();
		adata.setTrackerId(trackerId);
		adata.setAction("numberb");
		ParametersSetCache.getInstance().addAction(adata);		
	}else if(action.equalsIgnoreCase("numberc")){
		Alarm al = new Alarm();
		al.setAlarmType(AlarmType.Number_C.name());
		al.setCreateTime(new Date());
		al.setData(data);
		al.setState(work.equals(NumberCConfig.ENABLE) ? "Enabled":"Disabled");
		al.setTrackerId(trackerId);
		alarmService.addOrUpdateAlarm(al);
		
		NumberCConfig cfg = new NumberCConfig();
	 	cfg.setWork(work);
	 	cfg.setTel(data);
		if(ProcessorPool.getInstance().getProcessorByTrackerId(trackerId) != null){
			ProcessorPool.getInstance().getProcessorByTrackerId(trackerId).addConfig(cfg);
		}
		ActionData adata = new ActionData();
		adata.setTrackerId(trackerId);
		adata.setAction("numberc");
		ParametersSetCache.getInstance().addAction(adata);		
	}else if(action.equalsIgnoreCase("numberd")){
		Alarm al = new Alarm();
		al.setAlarmType(AlarmType.Number_D.name());
		al.setCreateTime(new Date());
		al.setData(data);
		al.setState(work.equals(NumberCConfig.ENABLE) ? "Enabled":"Disabled");
		al.setTrackerId(trackerId);
		alarmService.addOrUpdateAlarm(al);
		
		NumberDConfig cfg = new NumberDConfig();
	 	cfg.setWork(work);
	 	cfg.setTel(data);
		if(ProcessorPool.getInstance().getProcessorByTrackerId(trackerId) != null){
			ProcessorPool.getInstance().getProcessorByTrackerId(trackerId).addConfig(cfg);
		}
		ActionData adata = new ActionData();
		adata.setTrackerId(trackerId);
		adata.setAction("numberd");
		ParametersSetCache.getInstance().addAction(adata);		
	}else if(action.equalsIgnoreCase("reqLocation")){
		
		
		RequestLocationConfig cfg = new RequestLocationConfig();

		if(ProcessorPool.getInstance().getProcessorByTrackerId(trackerId) != null){
			ProcessorPool.getInstance().getProcessorByTrackerId(trackerId).addConfig(cfg);
		}
		ActionData adata = new ActionData();
		adata.setTrackerId(trackerId);
		adata.setAction("reqLocation");
		ParametersSetCache.getInstance().addAction(adata);		
	}else if(action.equalsIgnoreCase("reqStatus")){
		
		
		RequestStatusConfig cfg = new RequestStatusConfig();

		if(ProcessorPool.getInstance().getProcessorByTrackerId(trackerId) != null){
			ProcessorPool.getInstance().getProcessorByTrackerId(trackerId).addConfig(cfg);
		}
		ActionData adata = new ActionData();
		adata.setTrackerId(trackerId);
		adata.setAction("reqStatus");
		ParametersSetCache.getInstance().addAction(adata);		
	}else if(action.equalsIgnoreCase("timezone")){
		Alarm al = new Alarm();
		al.setAlarmType(AlarmType.Set_Time_Zone.name());
		al.setCreateTime(new Date());
		al.setData(data);
		al.setState(work.equals(TimeZoneConfig.ENABLE) ? "Enabled":"Disabled");
		al.setTrackerId(trackerId);
		alarmService.addOrUpdateAlarm(al);
		
		TimeZoneConfig cfg = new TimeZoneConfig();
		cfg.setWork(work);
		cfg.setTimeZone(data);

		if(ProcessorPool.getInstance().getProcessorByTrackerId(trackerId) != null){
			ProcessorPool.getInstance().getProcessorByTrackerId(trackerId).addConfig(cfg);
		}
		ActionData adata = new ActionData();
		adata.setTrackerId(trackerId);
		adata.setAction("timezone");
		ParametersSetCache.getInstance().addAction(adata);		
	}else if(action.equalsIgnoreCase("saveCoordinate")){
		SaveCoordinateConfig cfg = new SaveCoordinateConfig();
		cfg.setWork(work);
		if(ProcessorPool.getInstance().getProcessorByTrackerId(trackerId) != null){
			ProcessorPool.getInstance().getProcessorByTrackerId(trackerId).addConfig(cfg);
		}
		ActionData adata = new ActionData();
		adata.setTrackerId(trackerId);
		adata.setAction("saveCoordinate");
		ParametersSetCache.getInstance().addAction(adata);		
	}else if(action.equalsIgnoreCase("powerLost")){
		Alarm al = new Alarm();
		al.setAlarmType(AlarmType.External_Power_Lost.name());
		al.setCreateTime(new Date());
		al.setState(data);
		al.setTrackerId(trackerId);
		alarmService.addOrUpdateAlarm(al);		
		
		ExternalPowerLostConfig cfg = new ExternalPowerLostConfig();
		cfg.setData(work);

		if(ProcessorPool.getInstance().getProcessorByTrackerId(trackerId) != null){
			ProcessorPool.getInstance().getProcessorByTrackerId(trackerId).addConfig(cfg);
		}
		ActionData adata = new ActionData();
		adata.setTrackerId(trackerId);
		adata.setAction("powerLost");
		ParametersSetCache.getInstance().addAction(adata);		
	}else if(action.equalsIgnoreCase(ActionType.Listen_In)){
		ListenInConfig cfg = new ListenInConfig();
		cfg.setData(work);

		if(ProcessorPool.getInstance().getProcessorByTrackerId(trackerId) != null){
			ProcessorPool.getInstance().getProcessorByTrackerId(trackerId).addConfig(cfg);
		}
		ActionData adata = new ActionData();
		adata.setTrackerId(trackerId);
		adata.setAction(ActionType.Listen_In);
		ParametersSetCache.getInstance().addAction(adata);		
		
	}else if(action.equalsIgnoreCase(ActionType.Movement_Alert)){
		
		Alarm al = new Alarm();
		al.setAlarmType(AlarmType.Movement_Alert.name());
		al.setCreateTime(new Date());
		al.setData(data);
		System.out.println(data+"  data == 888");
		al.setTrackerId(trackerId);
		alarmService.addOrUpdateAlarm(al);
		
		MoveAlertConfig cfg = new MoveAlertConfig();
		cfg.setData(work);

		if(ProcessorPool.getInstance().getProcessorByTrackerId(trackerId) != null){
			ProcessorPool.getInstance().getProcessorByTrackerId(trackerId).addConfig(cfg);
		}
		ActionData adata = new ActionData();
		adata.setTrackerId(trackerId);
		adata.setAction(ActionType.Movement_Alert);
		ParametersSetCache.getInstance().addAction(adata);	
		
	}else if(action.equalsIgnoreCase(ActionType.Output)){
		OutputConfig cfg = new OutputConfig();
		cfg.setData(work);

		if(ProcessorPool.getInstance().getProcessorByTrackerId(trackerId) != null){
			ProcessorPool.getInstance().getProcessorByTrackerId(trackerId).addConfig(cfg);
		}
		ActionData adata = new ActionData();
		adata.setTrackerId(trackerId);
		adata.setAction(ActionType.Output);
		ParametersSetCache.getInstance().addAction(adata);		
		
	}else if(action.equalsIgnoreCase(ActionType.THIRD_PARTY_ACTION_set_authorized_numbers)){
		SetAuthNumbers num = new SetAuthNumbers();
		num.setContent(data);
		if(ProcessorPool.getInstance().getProcessorByTrackerId(trackerId) != null){
			ProcessorPool.getInstance().getProcessorByTrackerId(trackerId).addConfig(num);
		}
		ActionData adata = new ActionData();
		adata.setTrackerId(trackerId);
		adata.setAction(ActionType.THIRD_PARTY_ACTION_set_authorized_numbers);
		ParametersSetCache.getInstance().addAction(adata);	
		
	}else if(action.equalsIgnoreCase(ActionType.THIRD_PARTY_ACTION_delete_authorized_numbers)){
		DelAuthNumbers num = new DelAuthNumbers();
		num.setContent(data);
		if(ProcessorPool.getInstance().getProcessorByTrackerId(trackerId) != null){
			ProcessorPool.getInstance().getProcessorByTrackerId(trackerId).addConfig(num);
		}
		ActionData adata = new ActionData();
		adata.setTrackerId(trackerId);
		adata.setAction(ActionType.THIRD_PARTY_ACTION_delete_authorized_numbers);
		ParametersSetCache.getInstance().addAction(adata);	
		
	}else if(action.equalsIgnoreCase(ActionType.THIRD_PARTY_ACTION_locate_one_time)){
		LocateOneTime lt =new LocateOneTime();
		if(ProcessorPool.getInstance().getProcessorByTrackerId(trackerId) != null){
			ProcessorPool.getInstance().getProcessorByTrackerId(trackerId).addConfig(lt);
		}
		ActionData adata = new ActionData();
		adata.setTrackerId(trackerId);
		adata.setAction(ActionType.THIRD_PARTY_ACTION_locate_one_time);
		ParametersSetCache.getInstance().addAction(adata);	
		
	}else if(action.equalsIgnoreCase(ActionType.THIRD_PARTY_ACTION_continuous_track)){
		ContinuousTracking ct = new ContinuousTracking();
		ct.setContent(data);
		if(ProcessorPool.getInstance().getProcessorByTrackerId(trackerId) != null){
			ProcessorPool.getInstance().getProcessorByTrackerId(trackerId).addConfig(ct);
		}
		ActionData adata = new ActionData();
		adata.setTrackerId(trackerId);
		adata.setAction(ActionType.THIRD_PARTY_ACTION_continuous_track);
		ParametersSetCache.getInstance().addAction(adata);	
		
		
	}else if(action.equalsIgnoreCase(ActionType.THIRD_PARTY_ACTION_over_speed_alarm)){
		OverTimeSpeed ot = new OverTimeSpeed();
		ot.setContent(data);
		if(ProcessorPool.getInstance().getProcessorByTrackerId(trackerId) != null){
			ProcessorPool.getInstance().getProcessorByTrackerId(trackerId).addConfig(ot);
		}
		ActionData adata = new ActionData();
		adata.setTrackerId(trackerId);
		adata.setAction(ActionType.THIRD_PARTY_ACTION_over_speed_alarm);
		ParametersSetCache.getInstance().addAction(adata);	
		
	}else if(action.equalsIgnoreCase(ActionType.THIRD_PARTY_ACTION_time_zone)){
		SetTimeZone tz = new SetTimeZone();
		tz.setContent(data);
		if(ProcessorPool.getInstance().getProcessorByTrackerId(trackerId) != null){
			ProcessorPool.getInstance().getProcessorByTrackerId(trackerId).addConfig(tz);
		}
		ActionData adata = new ActionData();
		adata.setTrackerId(trackerId);
		adata.setAction(ActionType.THIRD_PARTY_ACTION_time_zone);
		ParametersSetCache.getInstance().addAction(adata);	
		
	}else if(action.equalsIgnoreCase(ActionType.THIRD_PARTY_ACTION_low_battery_alarm)){
		LowBatteryAlarm lb = new LowBatteryAlarm();
		lb.setContent(data);
		if(ProcessorPool.getInstance().getProcessorByTrackerId(trackerId) != null){
			ProcessorPool.getInstance().getProcessorByTrackerId(trackerId).addConfig(lb);
		}
		ActionData adata = new ActionData();
		adata.setTrackerId(trackerId);
		adata.setAction(ActionType.THIRD_PARTY_ACTION_low_battery_alarm);
		ParametersSetCache.getInstance().addAction(adata);	
		
	}else if(action.equalsIgnoreCase(ActionType.THIRD_PARTY_ACTION_modify_pass)){
		ModifyPassword mp = new ModifyPassword();
		mp.setContent(data);
		if(ProcessorPool.getInstance().getProcessorByTrackerId(trackerId) != null){
			ProcessorPool.getInstance().getProcessorByTrackerId(trackerId).addConfig(mp);
		}
		ActionData adata = new ActionData();
		adata.setTrackerId(trackerId);
		adata.setAction(ActionType.THIRD_PARTY_ACTION_modify_pass);
		ParametersSetCache.getInstance().addAction(adata);	
		
	}else if(action.equalsIgnoreCase(ActionType.THIRD_PARTY_ACTION_cut_acc_off)){
		CutAccOff ca = new CutAccOff();
		if(ProcessorPool.getInstance().getProcessorByTrackerId(trackerId) != null){
			ProcessorPool.getInstance().getProcessorByTrackerId(trackerId).addConfig(ca);
		}
		ActionData adata = new ActionData();
		adata.setTrackerId(trackerId);
		adata.setAction(ActionType.THIRD_PARTY_ACTION_cut_acc_off);
		ParametersSetCache.getInstance().addAction(adata);	
		
	}else if(action.equalsIgnoreCase(ActionType.THIRD_PARTY_ACTION_recover_acc)){
		RecoverAcc acc = new RecoverAcc();
		if(ProcessorPool.getInstance().getProcessorByTrackerId(trackerId) != null){
			ProcessorPool.getInstance().getProcessorByTrackerId(trackerId).addConfig(acc);
		}
		ActionData adata = new ActionData();
		adata.setTrackerId(trackerId);
		adata.setAction(ActionType.THIRD_PARTY_ACTION_recover_acc);
		ParametersSetCache.getInstance().addAction(adata);
		
	}else if(action.equalsIgnoreCase(ActionType.THIRD_PARTY_ACTION_switch_listen_mode)){
		SwitchToListen lis = new SwitchToListen();
		if(ProcessorPool.getInstance().getProcessorByTrackerId(trackerId) != null){
			ProcessorPool.getInstance().getProcessorByTrackerId(trackerId).addConfig(lis);
		}
		ActionData adata = new ActionData();
		adata.setTrackerId(trackerId);
		adata.setAction(ActionType.THIRD_PARTY_ACTION_switch_listen_mode);
		ParametersSetCache.getInstance().addAction(adata);
		
	}else if(action.equalsIgnoreCase(ActionType.THIRD_PARTY_ACTION_switch_talk_mode)){
		SwitchToTalk st = new SwitchToTalk();
		if(ProcessorPool.getInstance().getProcessorByTrackerId(trackerId) != null){
			ProcessorPool.getInstance().getProcessorByTrackerId(trackerId).addConfig(st);
		}
		ActionData adata = new ActionData();
		adata.setTrackerId(trackerId);
		adata.setAction(ActionType.THIRD_PARTY_ACTION_switch_talk_mode);
		ParametersSetCache.getInstance().addAction(adata);
		
	}else if(action.equalsIgnoreCase(ActionType.THIRD_PARTY_ACTION_apn_ip_port)){
		SetAPNConfig apn = new SetAPNConfig();
		apn.setContent(data);
		if(ProcessorPool.getInstance().getProcessorByTrackerId(trackerId) != null){
			ProcessorPool.getInstance().getProcessorByTrackerId(trackerId).addConfig(apn);
		}
		ActionData adata = new ActionData();
		adata.setTrackerId(trackerId);
		adata.setAction(ActionType.THIRD_PARTY_ACTION_apn_ip_port);
		ParametersSetCache.getInstance().addAction(adata);
		
	}else if(action.equalsIgnoreCase(ActionType.THIRD_PARTY_ACTION_apn_user)){
		SetAPNUser apn = new SetAPNUser();
		apn.setContent(data);
		if(ProcessorPool.getInstance().getProcessorByTrackerId(trackerId) != null){
			ProcessorPool.getInstance().getProcessorByTrackerId(trackerId).addConfig(apn);
		}
		ActionData adata = new ActionData();
		adata.setTrackerId(trackerId);
		adata.setAction(ActionType.THIRD_PARTY_ACTION_apn_user);
		ParametersSetCache.getInstance().addAction(adata);
	}
	
	
	
	
}
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
<link rel="stylesheet" type="text/css" media="all" href="../style.css">
<script type="text/javascript" src="../js/ext-base.js"></script>
    <script type="text/javascript" src="../js/ext-all.js"></script>
<script>
function close(){
	parent.hidePopWin(false);
}

function status(){
	Ext.Ajax.request({
		   url: 'foo.php',
		   success: someFn,
		   failure: otherFn,
		   headers: {
		       'my-header': 'foo'
		   },
		   params: { foo: 'bar' }
		});
}
</script>
</head>
<body onload="setTimeout('close();',3000);">
<br><br><br><br>
<center><span style="font-size:24px"><fmt:message key="control.setup.success"/></span></center>
</body>
</html>
<%
com.brother.database.DataBase.closeThreadConnection();
%>
